package com.shipwe.loans.ui.fragment

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.shipwe.loans.R
import com.shipwe.loans.databinding.FragmentOrderPayPinSuccessBinding
import com.shipwe.loans.pro.ShipweFragment
import com.shipwe.loans.ui.activity.ActivityOrderPayPin
import com.shipwe.loans.utils.event.AppEventPointType
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/27 17:04
 * @Annotation：支付成功
 */
class FragmentOrderPayPinSuccess(): ShipweFragment() {
    lateinit var binding: FragmentOrderPayPinSuccessBinding
    private var act: ActivityOrderPayPin? = null
    private var orderData: JSONObject? = null

    constructor(act: ActivityOrderPayPin, data: JSONObject): this(){
        this.act = act
        this.orderData = data
    }

    override fun getShipweRootView(inflater: LayoutInflater): View {
        binding = FragmentOrderPayPinSuccessBinding.inflate(inflater)
        return binding.root
    }

    override fun initShipweView() {
        rootViewBinding.rootLayout.setBackgroundResource(R.drawable.page_login_bg)
        setShipweTitleBarVisibility(View.VISIBLE)
        setShipweTextTitle(getString(R.string.loan_payment_status_title))
        setShipweRightBtnRes(R.drawable.customer_black)
    }

    override fun initShipweListener() {
        //客服
        rootViewBinding.rootCustomer.setOnClickListener {
            if (act != null){
                showCustomerDialog("PIN PaymentSuccess Page",act!!.getOrderPayProductName(),act!!.getOrderPayProductToken())
            }
            appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_CUSTOMER, "PIN PaymentSuccess Page")
        }
        binding.orderPaySuccessIdCopy.setOnClickListener {
            onOrderPaySuccessCopyAction(orderData!!.optString("transactionId"))
        }

        binding.orderPaySuccessApply.setOnClickListener {
            appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_LOAN_PAYMENT_SUCCESS_APPLY_AGAIN,"TransactionId：${orderData!!.optString("transactionId")}")
            act?.showOrderPaySuccessAction()
        }
    }

    override fun initShipweData() {
        refreshOrderPaySuccessData(orderData!!)
    }

    /**
     * 刷新数据
     */
    private fun refreshOrderPaySuccessData(data: JSONObject){
        orderData = data
        binding.orderPaySuccessAmount.text = data.optString("realRepayAmount","")
        binding.orderPaySuccessProductName.text = data.optString("product","")
        binding.orderPaySuccessThirdOrderId.text = data.optString("transactionId","")
        binding.orderPaySuccessTime.text = data.optString("realRepayTime","")
    }

    /**
     * 复制
     */
    private fun onOrderPaySuccessCopyAction(orderId: String){
        appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_LOAN_PAYMENT_SUCCESS_COPY, "TransactionId：$orderId")
        // 获取剪贴板管理器
        val clipboardManager = act!!.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        // 创建一个 ClipData 对象，将要复制的文本放入其中
        val clipData = ClipData.newPlainText("label", orderId)
        // 将 ClipData 对象放入剪贴板
        clipboardManager.setPrimaryClip(clipData)
        showShipweToast(getString(R.string.customer_email_copy_success))
    }

    override fun onStart() {
        super.onStart()
        if (act != null){
            appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_LOAN_PIN_REPAYMENT_SUCCESS_START,act!!.getOrderPayProductName())
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (act != null){
            appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_LOAN_PIN_REPAYMENT_SUCCESS_END,act!!.getOrderPayProductName())
        }
    }
}